//
//  ViewController.m
//  ZKSegmentDemo
//
//  Created by 王文壮 on 15/11/9.
//  Copyright © 2015年 WangWenzhuang. All rights reserved.
//

#import "homepage.h"
#import "Single.h"
#import "AFNetworking.h"
#import "Masonry.h"
#import "blue.h"
#import "mycell.h"
#import "publiccell.h"
#import "AddNewMes.h"
#import "MJRefresh.h"
#import "testaddpic.h"
#import <objc/runtime.h>
#import "editmes.h"
#import "QueryInformationViewController.h"


#define kScreenWidth ([UIScreen mainScreen].bounds.size.width)
#define kNumber (self.segmentedControl.numberOfSegments)
@interface homepage()<UIScrollViewDelegate>
@property (nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong)UISegmentedControl *segmentedControl;
@property(nonatomic,strong)UIButton * addmes;
@property (nonatomic,strong)UICollectionView * left;
@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic,strong)UICollectionView * right;
@property int check;
@property int check_empty;
@end

@implementation homepage

-(void)viewWillAppear:(BOOL)animated{
    [[Single sharedInstance].selfdatalist removeAllObjects];
     [[Single sharedInstance].dataimagelist removeAllObjects];
    NSString * url = @"http://172.18.178.56/api/content/texts/self";
       AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
       manager.responseSerializer = [AFJSONResponseSerializer serializer];
       manager.requestSerializer = [AFJSONRequestSerializer serializer];
    ((AFJSONResponseSerializer *)manager.responseSerializer).removesKeysWithNullValues = YES;
       [manager GET:url parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
           
       } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
           NSDictionary * returnjson = (NSDictionary *)responseObject;
          
           NSDictionary * Data =(NSDictionary * )returnjson[@"Data"];
           

           
           for(id key in Data){
               NSDictionary *temp = [[NSDictionary alloc]init];
               NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
               [formatter setDateStyle:NSDateFormatterMediumStyle];
               [formatter setTimeStyle:NSDateFormatterShortStyle];
               [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
               // 时间戳转时间的方法
               NSString *s = key[@"EditDate"];
               NSLog(@"%@",s);
               long long  myInt = [s longLongValue];
               NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:(myInt/1000/*+57600*/)];
               NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
               //
               NSString *cnum = [NSString stringWithFormat:@"%@",key[@"CommentNum"]];
               NSString *lnum = [NSString stringWithFormat:@"%@",key[@"LikeNum"]];
               temp = @{@"Date":confromTimespStr,
                        @"Name":key[@"Name"],
                         @"OwnId":key[@"OwnID"],
                        @"Detail":key[@"Detail"],
                        @"CommentNum":cnum,
                        @"LikeNum":lnum,
                        @"ID":key[@"ID"]
                        
                     
                        
                        
                        
               };
               
               [[Single sharedInstance].selfdatalist addObject:temp];
           }
           [_left reloadData];
           [_right reloadData];
           _check_empty = 1;
       } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
           
       }];
    
    NSString * url3 = @"http://172.18.178.56/api/content/album/self";
    AFHTTPSessionManager * manager3 = [AFHTTPSessionManager manager];
    manager3.responseSerializer = [AFJSONResponseSerializer serializer];
    manager3.requestSerializer = [AFJSONRequestSerializer serializer];
    ((AFJSONResponseSerializer *)manager3.responseSerializer).removesKeysWithNullValues = YES;
    [manager3 GET:url3 parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary * returnjson = (NSDictionary *)responseObject;
        NSDictionary * Data =(NSDictionary * )returnjson[@"Data"];
        for(id key in Data){
            NSMutableArray * dic_image = [[NSMutableArray alloc]init];
            NSDictionary * album =(NSDictionary *)key[@"Album"][@"Images"];
            
            for(id key2 in album){
                NSString * temp_iamge_url = [[NSString alloc] initWithFormat:@"%@%@",@"http://172.18.178.56/api/thumb/",key2[@"Thumb"]];
                [dic_image addObject:temp_iamge_url];
               
            }
            
            NSDictionary *temp = [[NSDictionary alloc]init];
            
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateStyle:NSDateFormatterMediumStyle];
            [formatter setTimeStyle:NSDateFormatterShortStyle];
            [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
            // 时间戳转时间的方法
            NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:(1606390481+57600)];
            NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
            NSString *cnum = [NSString stringWithFormat:@"%@",key[@"CommentNum"]];
            NSString *lnum = [NSString stringWithFormat:@"%@",key[@"LikeNum"]];
            temp = @{@"Date":confromTimespStr,
                     @"OwnId":key[@"OwnID"],
                     @"Name":key[@"Name"],
                     @"Detail":key[@"Detail"],
                     @"CommentNum":cnum,
                     @"LikeNum":lnum,
                     @"ID":key[@"ID"],
                     @"image":dic_image
                     
            };
            [[Single sharedInstance].dataimagelist addObject:temp];
        }
        [_left reloadData];
        [_right reloadData];
        _check_empty = 1;
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}


- (void)viewDidLoad {
    _check_empty = 0;
    [super viewDidLoad];
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:135/255.0 green:206/255.0 blue:250/255.0 alpha:1];
    
    _addmes = [[UIButton alloc] init];

    
     [_addmes setImage:[UIImage imageNamed:@"put"] forState:UIControlStateNormal];
    _addmes.layer.borderWidth = 2;
    
    
    
    
    
    
    
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    NSArray *array = [NSArray arrayWithObjects:@"文字内容",@"相册内容",nil];
       //初始化UISegmentedControl
       UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:array];
       //设置frame
   segment.selectedSegmentIndex = 0;
       segment.frame = CGRectMake(10, 100, self.view.frame.size.width, 50);
       //添加到视图
    [segment addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:segment];
    self.navigationItem.titleView = segment;
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.itemSize = CGSizeMake(180, 150);
    UICollectionViewFlowLayout * layout2 = [[UICollectionViewFlowLayout alloc]init];
    layout2.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout2.itemSize = CGSizeMake(180, 150);
    self.left =  [[UICollectionView alloc]initWithFrame:self.view.frame  collectionViewLayout:layout];
    self.right = [[UICollectionView alloc]initWithFrame:self.view.frame  collectionViewLayout:layout2];
    _left.delegate=self;
    _left.dataSource=self;
    
    [_left registerClass:[mycell class] forCellWithReuseIdentifier:@"cellid"];
    [_left registerClass:[publiccell class] forCellWithReuseIdentifier:@"cellimage"];
    
    _right.delegate=self;
    _right.dataSource=self;
    [_right registerClass:[mycell class] forCellWithReuseIdentifier:@"cellid"];
    [_right registerClass:[publiccell class] forCellWithReuseIdentifier:@"cellimage"];
    [self.view addSubview:_left];
    _left.backgroundColor = [UIColor whiteColor];
       [_left mas_makeConstraints:^(MASConstraintMaker *make) {
           make.centerX.equalTo(self.view.mas_centerX);
           make.width.equalTo(self.view.mas_width).multipliedBy(0.9);
           make.height.equalTo(self.view.mas_height);
           [_right removeFromSuperview];
       }];
    
    [_left reloadData];
    [_right reloadData];
    [self.view addSubview:_addmes];
   
    
    [_addmes mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom).with.offset(-self.view.frame.size.height*0.2);
        make.right.equalTo(self.view.mas_right).with.offset(-self.view.frame.size.width*0.1);
        make.height.equalTo(self.view.mas_width).multipliedBy(0.15);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.15);
    }];
    _addmes.layer.cornerRadius = self.view.frame.size.width*0.075;
    [_addmes addTarget:self action:@selector(addnewmes:) forControlEvents:UIControlEventTouchUpInside];

     _left.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    _right.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
       // 设置结果更新代理
    
    
    
    
    
    
}
-(void)refreshData{
    
  [self viewWillAppear:YES];
  [_left.mj_header endRefreshing];
  [_right.mj_header endRefreshing];
}


-(void)addnewmes:(id)sender{
    AddNewMes * newpage = [[AddNewMes alloc]init];
    [self.navigationController pushViewController:newpage animated:YES];
}

-(void)change:(UISegmentedControl *)sender{
    if (sender.selectedSegmentIndex == 0) {
        [self.view addSubview:_left];
        _left.backgroundColor = [UIColor whiteColor];
        [_left mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view.mas_centerX);
            make.width.equalTo(self.view.mas_width).multipliedBy(0.9);
            make.height.equalTo(self.view.mas_height);
           
        }];
         [_right removeFromSuperview];
        [self.view addSubview:_addmes];
        [_addmes setImage:[UIImage imageNamed:@"put"] forState:UIControlStateNormal];
        _addmes.layer.borderWidth = 2;
        [_addmes mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.view.mas_bottom).with.offset(-self.view.frame.size.height*0.2);
            make.right.equalTo(self.view.mas_right).with.offset(-self.view.frame.size.width*0.1);
            make.height.equalTo(self.view.mas_width).multipliedBy(0.15);
            make.width.equalTo(self.view.mas_width).multipliedBy(0.15);
        }];
        [_addmes addTarget:self action:@selector(addnewmes:) forControlEvents:UIControlEventTouchUpInside];
    _check = 0;
    }
    else if (sender.selectedSegmentIndex == 1){
        [self.view addSubview:_right];
        _right.backgroundColor = [UIColor whiteColor];
        [_right mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view.mas_centerX);
            make.width.equalTo(self.view.mas_width).multipliedBy(0.9);
            make.height.equalTo(self.view.mas_height);
        }];
        [_left removeFromSuperview];
        [self.view addSubview:_addmes];
               [_addmes setImage:[UIImage imageNamed:@"put"] forState:UIControlStateNormal];
        _addmes.layer.borderWidth = 2;
               [_addmes mas_makeConstraints:^(MASConstraintMaker *make) {
                   make.bottom.equalTo(self.view.mas_bottom).with.offset(-self.view.frame.size.height*0.2);
                   make.right.equalTo(self.view.mas_right).with.offset(-self.view.frame.size.width*0.1);
                   make.height.equalTo(self.view.mas_width).multipliedBy(0.15);
                   make.width.equalTo(self.view.mas_width).multipliedBy(0.15);
               }];
               [_addmes addTarget:self action:@selector(addnewmes:) forControlEvents:UIControlEventTouchUpInside];
        _check = 1;
    }
}
#pragma mark <UICollectionViewDataSource>

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView * )collectionView{
    [_left reloadData];
    [_right reloadData];
    if(_check == 0){
        if([Single sharedInstance].selfdatalist.count%2 == 0){
            return [Single sharedInstance].selfdatalist.count/2;
        }
        else{
            return [Single sharedInstance].selfdatalist.count/2+1;
        }
    }
    else{
      if([Single sharedInstance].dataimagelist.count%2 == 0){
            return [Single sharedInstance].dataimagelist.count/2;
        }
        else{
            return [Single sharedInstance].dataimagelist.count/2+1;
        }
    }
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 2;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
     mycell * cell;
    if(_check == 0){
        if(_check_empty == 1){
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellid" forIndexPath:indexPath];
            NSDictionary * temp ;
            int pos =indexPath.section*2+indexPath.row;
            if(pos>=[Single sharedInstance].selfdatalist.count){
                cell.synopsis.text = @"";
                cell.title.text = @"";
                cell.layer.borderColor = [UIColor whiteColor].CGColor;
                  cell.layer.borderWidth = 0;
                [cell.Delete setImage:nil forState:UIControlStateNormal];
                [cell.edit setImage:nil forState:UIControlStateNormal];
            }
            else{
                temp = [Single sharedInstance].selfdatalist[pos];
                NSString *t1 =[[NSString alloc]initWithFormat:@"%@ %@",@"  ",temp[@"Detail"]];
                NSString *t2 =[[NSString alloc]initWithFormat:@"%@ %@",@"  ",temp[@"Name"]];
                
                cell.synopsis.text = t1;
                cell.title.text = t2;
                cell.layer.borderColor = [UIColor blackColor].CGColor;
                cell.layer.borderWidth = 2;
                cell.layer.cornerRadius = 20;
                [cell.Delete setImage:[UIImage imageNamed:@"delete"] forState:UIControlStateNormal];
                [cell.edit setImage:[UIImage imageNamed:@"edit"] forState:UIControlStateNormal];
            }
            
        }
        else{
            
             cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellid" forIndexPath:indexPath];
        }
        
        NSMutableDictionary *nsDic = [[NSMutableDictionary alloc]init];
        NSInteger section = indexPath.section;
        NSInteger row = indexPath.row;
        NSNumber *numbersec = [NSNumber numberWithInt:section];
        NSNumber *numberrow = [NSNumber numberWithInt:row];
        [nsDic setObject:numbersec forKey:@"section"];

        [nsDic setObject:numberrow forKey:@"row"];
        
        objc_setAssociatedObject(cell.Delete, @"myBtn", nsDic, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        objc_setAssociatedObject(cell.edit, @"myBtn", nsDic, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        
        [cell.Delete addTarget:self action:@selector(deleteact:) forControlEvents:UIControlEventTouchUpInside];
        [cell.edit addTarget:self action:@selector(edit:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
   else  if(_check == 1){
      publiccell * cell2;
       cell2 = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellimage" forIndexPath:indexPath];
       if(_check_empty == 1){
           cell2 = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellimage" forIndexPath:indexPath];
           NSDictionary * temp ;
           int pos =indexPath.section*2+indexPath.row;
           if(pos>=[Single sharedInstance].dataimagelist.count){
               //cell2.synopsis.text = @"";
               cell2.title.text = @"";
               [cell.Delete setImage:nil forState:UIControlStateNormal];
               [cell.edit setImage:nil forState:UIControlStateNormal];
           }
           else{
               temp = [Single sharedInstance].dataimagelist[pos];
               NSString *t1 =[[NSString alloc]initWithFormat:@"%@ %@",@"  ",temp[@"Detail"]];
               NSString *t2 =[[NSString alloc]initWithFormat:@"%@ %@",@"  ",temp[@"Name"]];
               
               cell2.title.text = t2;
               NSDictionary * album =(NSDictionary *)temp[@"image"];
               NSString * fin_url;
               for(id key in album){
                   fin_url = key;
                   break;
               }
               if(fin_url != nil){
                   UIImage * header = [UIImage imageWithData:[NSData  dataWithContentsOfURL:[NSURL URLWithString:fin_url]]];
            
                   cell2.mainview.image =header;
               }
               else{
                   cell2.mainview.image = nil;
               }
               cell2.layer.borderColor = [UIColor blackColor].CGColor;
               cell2.layer.borderWidth = 2;
               cell2.layer.cornerRadius = 20;
               cell2.mainview.layer.cornerRadius = 20;
               cell2.layer.masksToBounds=YES;
               [cell2.Delete setImage:[UIImage imageNamed:@"delete"] forState:UIControlStateNormal];
               [cell2.edit setImage:[UIImage imageNamed:@"edit"] forState:UIControlStateNormal];
           }
       }
       else{
            cell2 = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellimage" forIndexPath:indexPath];
       }
       
       NSMutableDictionary *nsDic2 = [[NSMutableDictionary alloc]init];
       NSInteger section2 = indexPath.section;
       NSInteger row2 = indexPath.row;
       NSNumber *numbersec2 = [NSNumber numberWithInt:section2];
       NSNumber *numberrow2 = [NSNumber numberWithInt:row2];
       [nsDic2 setObject:numbersec2 forKey:@"section"];

       [nsDic2 setObject:numberrow2 forKey:@"row"];
       
       objc_setAssociatedObject(cell2.Delete, @"myBtn2", nsDic2, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
       objc_setAssociatedObject(cell2.edit, @"myBtn2", nsDic2, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
       
       [cell2.Delete addTarget:self action:@selector(deleteact2:) forControlEvents:UIControlEventTouchUpInside];
       
       [cell2.edit addTarget:self action:@selector(edit2:) forControlEvents:UIControlEventTouchUpInside];
       
       return  cell2;
    }
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    int section = indexPath.section;
    int row = indexPath.row;
    if(_check == 0){
        if(section*2+row >=[Single sharedInstance].selfdatalist.count){
            
        }
        else{
            int pos =indexPath.section*2+indexPath.row;
            [Single sharedInstance].commentid = [Single sharedInstance].selfdatalist[indexPath.section][@"ID"];
            [Single sharedInstance].fatherid = [Single sharedInstance].selfdatalist[indexPath.section][@"OwnId"];
            NSLog(@"%@", [Single sharedInstance].commentid);
            NSLog(@"%@", [Single sharedInstance].fatherid );
            CGRect screenFrame = [UIScreen mainScreen].bounds;
            int screenWidth = screenFrame.size.width;
            int screenHeight = screenFrame.size.height;
            QueryInformationViewController *newpage = [[QueryInformationViewController alloc] init];
            
            newpage.label1 = [[UILabel alloc]initWithFrame:CGRectMake(30, screenHeight/10-30, 250, 60)];
            newpage.label1.text = [Single sharedInstance].selfdatalist[pos][@"Date"];
            newpage.label2 = [[UILabel alloc]initWithFrame:CGRectMake(30, screenHeight/10+30, 100, 60)];
            newpage.label2.text = [Single sharedInstance].selfdatalist[pos][@"Name"];
            newpage.labelname = [[UILabel alloc]initWithFrame:CGRectMake(30, screenHeight/10, 100, 60)];
            newpage.labelname.text = @"";
            newpage.label3 = [[MPlabel alloc] init];
            [newpage.label3 setLabelWithOrigin:CGPointMake(30, screenHeight/10+80) mutableText:[Single sharedInstance].selfdatalist[pos][@"Detail"] mutableFont:[UIFont boldSystemFontOfSize:20] margin:0];
            
            newpage.viewww = [[UIView alloc]init];
            //imagelist
            newpage.imagelist = [Single sharedInstance].selfdatalist[pos][@"image"];
            //NSLog(@"%@",newpage.imagelist);
            //imagelist
            [self.navigationController pushViewController:newpage animated:YES];
        }
    }
    else{
        if(section*2+row >=[Single sharedInstance].dataimagelist.count){
            
        }
        else{
            int pos =indexPath.section*2+indexPath.row;
            [Single sharedInstance].commentid = [Single sharedInstance].dataimagelist[pos][@"ID"];
            [Single sharedInstance].fatherid = [Single sharedInstance].dataimagelist[pos][@"OwnId"];
           
            CGRect screenFrame = [UIScreen mainScreen].bounds;
            int screenWidth = screenFrame.size.width;
            int screenHeight = screenFrame.size.height;
            QueryInformationViewController *newpage = [[QueryInformationViewController alloc] init];
            
            newpage.label1 = [[UILabel alloc]initWithFrame:CGRectMake(30, screenHeight/10-30, 250, 60)];
            newpage.label1.text = [Single sharedInstance].dataimagelist[pos][@"Date"];
            newpage.label2 = [[UILabel alloc]initWithFrame:CGRectMake(30, screenHeight/10+30, 100, 60)];
            newpage.label2.text = [Single sharedInstance].dataimagelist[pos][@"Name"];
            newpage.labelname = [[UILabel alloc]initWithFrame:CGRectMake(30, screenHeight/10, 100, 60)];
            newpage.labelname.text = @"";
            newpage.label3 = [[MPlabel alloc] init];
            [newpage.label3 setLabelWithOrigin:CGPointMake(30, screenHeight/10+80) mutableText:[Single sharedInstance].dataimagelist[pos][@"Detail"] mutableFont:[UIFont boldSystemFontOfSize:20] margin:0];
            
            newpage.viewww = [[UIView alloc]init];
            //imagelist
            newpage.imagelist = [Single sharedInstance].dataimagelist[pos][@"image"];
            //NSLog(@"%@",newpage.imagelist);
            //imagelist
            [self.navigationController pushViewController:newpage animated:YES];
        }
        
    }
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{
    return UIEdgeInsetsMake(15,0,15,0);

}

-(void)edit:(UIButton*)sender {
    NSDictionary *dic = objc_getAssociatedObject(sender, @"myBtn");
    int section = [[dic objectForKey:@"section"] intValue];
    int row = [[dic objectForKey:@"row"] intValue];
    //NSLog(@"%d %d",section,row);
    if(section*2+row>=[Single sharedInstance].selfdatalist.count){
        sender.backgroundColor = [UIColor whiteColor];
    }
    else{
        editmes * newpage = [[editmes alloc]init];
        newpage.idurl =  [Single sharedInstance].selfdatalist[section*2+row][@"ID"];
        [self.navigationController pushViewController:newpage animated:YES];
    }
    
}


-(void)edit2:(UIButton*)sender {
    NSDictionary *dic = objc_getAssociatedObject(sender, @"myBtn2");
    int section = [[dic objectForKey:@"section"] intValue];
    int row = [[dic objectForKey:@"row"] intValue];
    //NSLog(@"%d %d",section,row);
    if(section*2+row>=[Single sharedInstance].dataimagelist.count){
        sender.backgroundColor = [UIColor whiteColor];
    }
    else{
        editmes * newpage = [[editmes alloc]init];
        newpage.idurl =  [Single sharedInstance].dataimagelist[section*2+row][@"ID"];
        [self.navigationController pushViewController:newpage animated:YES];
    }
    
}


-(void)deleteact:(UIButton*)sender {
    NSDictionary *dic = objc_getAssociatedObject(sender, @"myBtn");
    int section = [[dic objectForKey:@"section"] intValue];
    int row = [[dic objectForKey:@"row"] intValue];
    //NSLog(@"%d %d",section,row);
    if(section*2+row>=[Single sharedInstance].selfdatalist.count){
        sender.backgroundColor = [UIColor whiteColor];
    }
    else{
        
        UIAlertController * alert1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要删除吗?" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert1 animated:YES completion:nil];
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
                   NSString * deleteid = [Single sharedInstance].selfdatalist[section*2+row][@"ID"];
                    NSString * deleteurl =[[NSString alloc]initWithFormat:@"%@%@",@"http://172.18.178.56/api/content/",deleteid];
                    AFHTTPSessionManager * manager3 = [AFHTTPSessionManager manager];
                    manager3.responseSerializer = [AFJSONResponseSerializer serializer];
                    manager3.requestSerializer = [AFJSONRequestSerializer serializer];
                    [manager3 DELETE:deleteurl parameters:nil headers:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                        [[Single sharedInstance].selfdatalist removeObjectAtIndex:section*2+row];
                        UIAlertController * alert1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"删除成功" preferredStyle:UIAlertControllerStyleAlert];
                        [self presentViewController:alert1 animated:YES completion:nil];
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                            [self.presentedViewController dismissViewControllerAnimated:NO completion:nil];
                        });
                        [_left reloadData];
                        
                        
                    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                        
                    }];
            }];
        UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
                   
            }];
        [alert1 addAction:defaultAction];
        [alert1 addAction:cancelAction];
    }
}
-(void)deleteact2:(UIButton*)sender {
    NSDictionary *dic = objc_getAssociatedObject(sender, @"myBtn2");
    int section = [[dic objectForKey:@"section"] intValue];
    int row = [[dic objectForKey:@"row"] intValue];
    //NSLog(@"%d %d",section,row);
    if(section*2+row>=[Single sharedInstance].dataimagelist.count){
        sender.backgroundColor = [UIColor whiteColor];
    }
    else{
        
        UIAlertController * alert1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要删除吗?" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert1 animated:YES completion:nil];
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
                   NSString * deleteid = [Single sharedInstance].dataimagelist[section*2+row][@"ID"];
                    NSString * deleteurl =[[NSString alloc]initWithFormat:@"%@%@",@"http://172.18.178.56/api/content/",deleteid];
                    AFHTTPSessionManager * manager3 = [AFHTTPSessionManager manager];
                    manager3.responseSerializer = [AFJSONResponseSerializer serializer];
                    manager3.requestSerializer = [AFJSONRequestSerializer serializer];
                    [manager3 DELETE:deleteurl parameters:nil headers:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                        [[Single sharedInstance].dataimagelist removeObjectAtIndex:section*2+row];
                        UIAlertController * alert1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"删除成功" preferredStyle:UIAlertControllerStyleAlert];
                        [self presentViewController:alert1 animated:YES completion:nil];
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                            [self.presentedViewController dismissViewControllerAnimated:NO completion:nil];
                        });
                        [_right reloadData];
                        
                        
                    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                        
                    }];
            }];
        UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
                   
            }];
        [alert1 addAction:defaultAction];
        [alert1 addAction:cancelAction];
        
    }
}

-(void)senddelete{
    
}
@end

